using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.IO;
using System.Threading;

namespace cb0tServer
{
    class Translator
    {
        private Thread thread;
        private bool busy = false;
        private UserObject query = null;
        private String text = null;

        public void Query(UserObject query, String text)
        {
            if (busy) return;

            this.query = query;
            this.text = text;
            this.thread = new Thread(new ThreadStart(this.Work));
            this.thread.Start();
        }

        public String[] Languages()
        {
            List<String> tmp = new List<String>();
            tmp.Add("English to Arabic");
            tmp.Add("English to Dutch");
            tmp.Add("English to French");
            tmp.Add("English to German");
            tmp.Add("English to Greek");
            tmp.Add("English to Italian");
            tmp.Add("English to Portuguese");
            tmp.Add("English to Spanish");
            tmp.Add("English to Russian");
            tmp.Add("Arabic to English");
            tmp.Add("Dutch to English");
            tmp.Add("French to English");
            tmp.Add("German to English");
            tmp.Add("Greek to English");
            tmp.Add("Italian to English");
            tmp.Add("Portuguese to English");
            tmp.Add("Spanish to English");
            tmp.Add("Russian to English");
            return tmp.ToArray();
        }

        private String removeEntities(String str)
        {
            String _str = str;
            _str = _str.Replace("&quot;", "\"");
            _str = _str.Replace("&amp;", "&");
            _str = _str.Replace("&#160;", " ");
            _str = _str.Replace("&#39;", "'");
            _str = _str.Replace("&lt;", "<");
            _str = _str.Replace("&gt;", ">");
            return _str;
        }

        private String GetCode()
        {
            switch (this.query.translate_type)
            {
                case 0:
                    return "en|ar"; // English to Arabic
                case 1:
                    return "en|nl"; // English to Dutch
                case 2:
                    return "en|fr"; // English to French
                case 3:
                    return "en|de"; // English to German
                case 4:
                    return "en|el"; // English to Greek
                case 5:
                    return "en|it"; // English to Italian
                case 6:
                    return "en|pt"; // English to Portuguese
                case 7:
                    return "en|es"; // English to Spanish
                case 8:
                    return "en|ru"; // English to Russian
                case 9:
                    return "ar|en"; // Arabic to English
                case 10:
                    return "nl|en"; // Dutch to English
                case 11:
                    return "fr|en"; // French to English
                case 12:
                    return "de|en"; // German to English
                case 13:
                    return "el|en"; // Greek to English
                case 14:
                    return "it|en"; // Italian to English
                case 15:
                    return "pt|en"; // Portuguese to English
                case 16:
                    return "es|en"; // Spanish to English
                case 17:
                    return "ru|en"; // Russian to English
                default:
                    break;
            }

            return "";
        }

        private void Work()
        {
            this.busy = true;

            try
            {
                HttpWebRequest r = (HttpWebRequest)WebRequest.Create("http://www.google.com/translate_t?hl=en&ie=UTF8&text=" + Uri.EscapeDataString(this.text) + "&langpair=" + GetCode());
                r.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; WOW64; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; Media Center PC 5.0)";
                StreamReader s = new StreamReader(r.GetResponse().GetResponseStream(), Encoding.UTF8);
                String result = s.ReadToEnd();

                if (result.IndexOf("<div id=result_box dir=") > -1)
                {
                    result = result.Substring(result.IndexOf("<div id=result_box dir="));
                    result = result.Substring(result.IndexOf(">") + 1);
                    result = result.Substring(0, result.IndexOf("</div>"));
                    UserPool.Broadcast(this.query.vroom, ServerOutboundPackets.PublicTextPacket(this.query.name, "[translator] " + removeEntities(result)));
                }

                s.Close();
            }
            catch { }

            this.busy = false;
        }
    }
}
